<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class weeks extends MX_Controller {

    private $module = 'weeks';
    private $table = 'weeks';

    function __construct(){
		parent::__construct();
		$this->load->model($this->module.'_model','model');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.PATH_URL.'admincp/login');
					return false;
				}
			}
			$this->template->set_template('admin');
			$this->template->write('title','Admin Control Panel');
		}
	}

    /* ------------------------------------ Admin Control Panel ------------------------------------ */

    public function admincp_index() {
        $default_func = 'created';
        $default_sort = 'DESC';
        $data = array(
            'module' => $this->module,
            'module_name' => "Manage Weeks",
            'default_func' => $default_func,
            'default_sort' => $default_sort
        );
        $this->template->write_view('content', 'BACKEND/index', $data);
        $this->template->render();
    }

    public function admincp_update($id = 0) {
        $result[0] = array();
        if ($id != 0) {
            $result = $this->model->getDetailManagement($id);
        }
        $data = array(
            'result' => $result[0],
            'module' => $this->module,
            'id' => $id
        );
        $this->template->write_view('content', 'BACKEND/ajax_editContent', $data);
        $this->template->render();
    }

    public function admincp_save() {

		if($_POST){
			//Upload Image
			$fileName = "";
		
			//End Upload Image

			if($this->model->saveManagement($fileName)){
				print 'success';
				exit;
			}
		}
    }

    public function admincp_delete() {
        if ($perm == 'permission-denied') {
            print $perm;
            exit;
        }
        if ($this->input->post('id')) {
            $id = $this->input->post('id');
            $result = $this->model->getDetailManagement($id);
            modules::run('admincp/saveLog', $this->module, $id, 'delete', 'delete');
            $this->db->where('id', $id);
            if ($this->db->delete(PREFIX . $this->table)) {
                //Xóa hình khi Delete
                @unlink(DIR_UPLOAD_WEEKS . $result[0]->img_price);
                return true;
            }
        }
    }

    public function admincp_ajaxLoadContent() {
        $this->load->library('AdminPagination');
        $config['total_rows'] = $this->model->getTotalsearchContent();
        $config['per_page'] = $this->input->post('per_page');
        $config['num_links'] = 3;
        $config['func_ajax'] = 'searchContent';
        $config['start'] = $this->input->post('start');
        $this->adminpagination->initialize($config);

        $result = $this->model->getsearchContent($config['per_page'], $this->input->post('start'));
        $data = array(
            'result' => $result,
            'per_page' => $this->input->post('per_page'),
            'start' => $this->input->post('start'),
            'module' => $this->module
        );
        $this->session->set_userdata('start', $this->input->post('start'));
        $this->load->view('BACKEND/ajax_loadContent', $data);
    }

    public function admincp_ajaxUpdateStatus() {


            if ($this->input->post('status') == 0) {
                $status = 1;
            } else {
                $status = 0;
            }
            $data = array(
                'status' => $status
            );
            modules::run('admincp/saveLog', $this->module, $this->input->post('id'), 'status', 'update', $this->input->post('status'), $status);
            $this->db->where('id', $this->input->post('id'));
            $this->db->update(PREFIX . $this->table, $data);
        

        $update = array(
            'status' => $status,
            'id' => $this->input->post('id'),
            'module' => $this->module
        );
        $this->load->view('BACKEND/ajax_updateStatus', $update);
    }
     public function admincp_ajaxUpdateActive() {

            if ($this->input->post('active') == 0) {
                $active = 1;
            } else {
                $active = 0;
            }
            $data = array(
                'active' => $active
            );
            $this->db->where('id', $this->input->post('id'));
            $this->db->update(PREFIX . $this->table, $data);
        

        $update = array(
            'active' => $active,
            'id' => $this->input->post('id'),
            'module' => $this->module
        );
        $this->load->view('BACKEND/ajax_updateActive', $update);
    }
     public function admincp_ajaxUpdateFinal() {
    
            if ($this->input->post('finish') == 0) {
                $finish = 1;
            } else {
                $finish = 0;
            }
            $data = array(
                'finish' => $finish
            );
            modules::run('admincp/saveLog', $this->module, $this->input->post('id'), 'finish', 'update', $this->input->post('finish'), $finish);
            $this->db->where('id', $this->input->post('id'));
            $this->db->update(PREFIX . $this->table, $data);
        

        $update = array(
            'finish' => $finish,
            'id' => $this->input->post('id'),
            'module' => $this->module
        );
        $this->load->view('BACKEND/ajax_updateFinal', $update);
    }

    /* ------------------------------------ End Admin Control Panel -------------------------------- */

    function index() {
        $data=array();
        $this->template->write_view('content', 'FRONTEND/index',$data);
        $this->template->render();
    }
    
    function ajax_index() {
        $data=array();
        $this->load->view('FRONTEND/ajax_index',$data);
    }
    function prices(){
        $data=array();
        $data['prices'] = $this->model->getPrices();
        $data['chungcuoc'] = $this->model->getChungCuoc();
		$this->load->view('FRONTEND/prices',$data);
	}
}